package com.stylefeng.guns.modular.finance.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.system.model.CostLoan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.CostOnlineLoan;
import com.stylefeng.guns.modular.finance.service.ICostOnlineLoanService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 网贷收入控制器
 *
 * @author fengshuonan
 * @Date 2018-08-26 15:57:15
 */
@Controller
@RequestMapping("/costOnlineLoan")
public class CostOnlineLoanController extends BaseController {

    private String PREFIX = "/finance/costOnlineLoan/";

    @Autowired
    private ICostOnlineLoanService costOnlineLoanService;

    /**
     * 跳转到网贷收入首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "costOnlineLoan.html";
    }

    /**
     * 跳转到添加网贷收入
     */
    @RequestMapping("/costOnlineLoan_add")
    public String costOnlineLoanAdd() {
        return PREFIX + "costOnlineLoan_add.html";
    }

    /**
     * 跳转到修改网贷收入
     */
    @RequestMapping("/costOnlineLoan_update/{costOnlineLoanId}")
    public String costOnlineLoanUpdate(@PathVariable Integer costOnlineLoanId, Model model) {
        CostOnlineLoan costOnlineLoan = costOnlineLoanService.selectById(costOnlineLoanId);
        model.addAttribute("item",costOnlineLoan);
        LogObjectHolder.me().set(costOnlineLoan);
        return PREFIX + "costOnlineLoan_edit.html";
    }

    /**
     * 获取网贷收入列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        EntityWrapper<CostOnlineLoan> costServiceEntityWrapper = new EntityWrapper<>();
        costServiceEntityWrapper.like("company", condition);
        List<Map<String, Object>> mapList = costOnlineLoanService.selectMaps(costServiceEntityWrapper);
        BigDecimal total = new BigDecimal(0.00);
        for (Map<String, Object> map: mapList) {
            total = total.add((BigDecimal) map.get("amount"));
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("amount", total);
        mapList.add(0, map);
        return mapList;
    }

    /**
     * 新增网贷收入
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CostOnlineLoan costOnlineLoan) {
        costOnlineLoan.setCreateTime(new Date());
        costOnlineLoanService.insert(costOnlineLoan);
        return SUCCESS_TIP;
    }

    /**
     * 删除网贷收入
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer costOnlineLoanId) {
        costOnlineLoanService.deleteById(costOnlineLoanId);
        return SUCCESS_TIP;
    }

    /**
     * 修改网贷收入
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(CostOnlineLoan costOnlineLoan) {
        costOnlineLoanService.updateById(costOnlineLoan);
        return SUCCESS_TIP;
    }

    /**
     * 网贷收入详情
     */
    @RequestMapping(value = "/detail/{costOnlineLoanId}")
    @ResponseBody
    public Object detail(@PathVariable("costOnlineLoanId") Integer costOnlineLoanId) {
        return costOnlineLoanService.selectById(costOnlineLoanId);
    }
}
